package com.malbbmsomanager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DM_BBM_ApplicationDBLoader extends SQLiteOpenHelper {

	// Database Name.
	private static final String DATABASE_NAME = "dminvoicemanagerbbm.db";
	// Master Tables BBM
	private String FDEBTOR_TABLE = "fDebtor";
	private String FTAXDET_TABLE = "FtaxDet";
	private String FBRAND_TABLE = "FBrand";
	private String FITEMS_TABLE = "FItems";
	private String FCURRENCY_TABLE = "fCurrency";
	private String FSALREP_TABLE = "fSalRep";
	private String FCOMPANYSETTINGS_TABLE = "fCompanySetting";
	private String FLOCATION_TABLE = "fLocation";
	private String FITEMFREQTY_TABLE = "fItemFreeQty";
	private String FITEMPRI_TABLE = "fItemPri";
	private String FTAXHED_TABLE = "fTaxHed";
	private String FTAX_TABLE = "fTax";
	private String FDDBNOTE_TABLE = "fddbnote";
	private String FAREA_TABLE = "fArea";
	private String FCONTROL_TABLE = "fControlResult";
	private String FCOMPANYBRANCH_TABLE = "FCompanyBranch";
	private String FDEBTAX_TABLE = "fDebTax";
	private String FCOMPANYDM_TABLE = "fCompanyDM";
	private String ITEMMASTER_SUM = "FItemsMasterSum";
	private String SERVER_DB_LIST = "serverdb";
	private String USER_SETTINGS = "usersettings";
	private String YEARS_LIST = "FYearsList";
	private String MONTH_LIST = "FMonthList";
	private String SERVER_DETAILS = "serverinfo";
	private String FGLFACC_TABLE = "fglfacc";
	private String FBANK_TABLE = "fbank";

	// ****Main Transaction Table****
	private String FORDHED_TABLE = "FOrdHed";
	private String FORDDET_TABLE = "FOrdDet";
	private String FORDTAXDT_TABLE = "FOrdTaxDt";
	private String FORDTAXRG_TABLE = "FOrdTaxRg";

	// Receipt Tranactiob
	private String FPRECHED_TABLE = "FPRecHed";
	private String FPRECDET_TABLE = "FPRecDet";

	// Logcat tag
	private static final String LOG = "DatabaseHelper";

	// Database Version
	private static final int DATABASE_VERSION = 3;
	private static final String TAG = null;

	public DM_BBM_ApplicationDBLoader(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// this.myContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase mydb) {
		try {

			mydb.execSQL("create table if not exists "
					+ YEARS_LIST
					+ "(year_id integer primary key autoincrement,yearname text not null);");

			mydb.execSQL("create table if not exists "
					+ MONTH_LIST
					+ "(month_id integer primary key autoincrement,monthindex text not null,monthname text not null);");

			mydb.execSQL("create table if not exists "
					+ SERVER_DETAILS
					+ "(server_id integer primary key autoincrement,serverurl text not null,portval text not null);");

			mydb.execSQL("create table if not exists "
					+ SERVER_DB_LIST
					+ "(sdb_id integer primary key autoincrement,sdbname text not null);");

			mydb.execSQL("create table if not exists "
					+ USER_SETTINGS
					+ "(_id integer primary key autoincrement,username text not null,lrepprefix text not null,lreploccode text not null,currentStOrdNo text not null,locationCode text not null,receiptCode text not null,serverDbName text not null,printerMac text not null,repareacode text not null,repdealcode text not null,reproutecode text not null);");

			// Create Master Tables
			mydb.execSQL("create table if not exists "
					+ FDEBTOR_TABLE
					+ "(_id integer primary key autoincrement,DebCode text not null,DebName text not null,DebAdd1 text not null,DebAdd2 text not null,DebAdd3 text not null,DebTele text not null,DebMob text not null,DelEmail text not null,Status text not null,DelPersn text not null,TaxReg text not null,RecordId text not null,AreaCode text not null,CrdPeriod text not null,CrdLimit text not null,RepCode text not null,PrilCode text not null,AreaName text not null);");

			mydb.execSQL("create table if not exists "
					+ FTAXDET_TABLE
					+ "(ftaxdet_id integer primary key autoincrement,TaxComCode  text not null,TaxCode  text not null,TaxRate  text not null,TaxMode  text not null,TaxSeq  text not null,RecordId  text not null);");

			mydb.execSQL("create table if not exists "
					+ FBRAND_TABLE
					+ "(fbrand_id integer primary key autoincrement,BrandCode text not null,BrandName text not null,AddUser text not null,AddDate text not null,AddMach text not null,RecordId text not null);");

			mydb.execSQL("create table if not exists "
					+ FITEMS_TABLE
					+ "(_id integer primary key autoincrement,ItemCode text not null,ItemName text not null,BrandCode text not null,AvgPrice text not null,PrilCode text not null,packing text not null,PackSize text not null,TaxComCode text not null,ReOrderQty text not null,AddDate text not null,RecordId text not null,PurDesc text not null,TypeCode text not null,UnitCode text not null,ReOrderLvl text not null,CatCode text not null,cosacc text not null,GroupCode text not null,itemsummary text not null,ItemStatus text not null,FreeItems text not null);");

			mydb.execSQL("create table if not exists "
					+ FCURRENCY_TABLE
					+ "(fcur_id integer primary key autoincrement,CurCode text not null,CurDesc text not null,fromrate text not null,torate text not null,AddUser text not null,AddDate text not null,AddMach text not null,recordid  text not null);");

			mydb.execSQL("create table if not exists "
					+ FSALREP_TABLE
					+ "(fsalrep_id integer primary key autoincrement,RepCode text not null,RepName text not null,RepIdNo text not null,RepAdd1 text not null,RepAdd2 text not null,RepAdd3 text not null,RepTele text not null,RepMobil text not null,RepEMail text not null,AddUser text not null,AddMach text not null,AddDate text not null,RecordId  text not null,RepPrefix  text not null);");

			mydb.execSQL("create table if not exists "
					+ FCOMPANYSETTINGS_TABLE
					+ "(fcomset_id integer primary key autoincrement,cSettingsCode text not null,cSettingGrp text not null,cLocationChar text not null,cCharVal text not null,nNumVal text not null,cRemarks text not null,nType text not null,bUpdateFlag text not null,cCompanyCode text not null,recordid text not null);");

			mydb.execSQL("create table if not exists "
					+ FLOCATION_TABLE
					+ "(floc_id integer primary key autoincrement,LocCode text not null,LocName text not null,TeleNo text not null,FaxNo text not null,EMail text not null,LoctCode text not null,Contact text not null,Address1 text not null,Address2 text not null,Address3 text not null,AddUser text not null,AddDate text not null,AddMach text not null,RecordId text not null,prefix text not null,cSettingsCode text not null,ProfCen text not null,cSettingsCde2 text not null,CostCode text not null,AccCode text not null);");

			mydb.execSQL("create table if not exists "
					+ FITEMFREQTY_TABLE
					+ "(_id integer primary key autoincrement,ItemCode text not null,FreeQty text not null);");

			mydb.execSQL("create table if not exists "
					+ FITEMPRI_TABLE
					+ "(_id integer primary key autoincrement,ItemCode text not null,Price text not null,PrilCode text not null);");

			mydb.execSQL("create table if not exists "
					+ FTAXHED_TABLE
					+ "(taxhed_id integer primary key autoincrement,Active text not null,AddDate text not null,TaxComCode text not null,TaxComName text not null,RecordId text not null);");

			mydb.execSQL("create table if not exists "
					+ FTAX_TABLE
					+ "(tax_id integer primary key autoincrement,AddDate text not null,TaxCode text not null,TaxDevd text not null,TaxName text not null,TaxPayAcc text not null,TaxPer text not null,TaxRecAcc text not null,TaxRegNo text not null,RecordId text not null);");

			mydb.execSQL("create table if not exists "
					+ FCOMPANYBRANCH_TABLE
					+ "(branch_id integer primary key autoincrement,cSettingsCodefb text not null,BranchCode text not null,nNumValfb text not null,RecordId text not null,nMonthb text not null,nYearb text not null);");

			mydb.execSQL("create table if not exists "
					+ FDEBTAX_TABLE
					+ "(fdebtax_id integer primary key autoincrement,DebCode text not null,TaxCode text not null,TaxRegNo text not null,RecordId text not null);");

			mydb.execSQL("create table if not exists "
					+ FCOMPANYDM_TABLE
					+ "(Fcompanydm_id integer primary key autoincrement,cSettingsCodedm text not null,nMonth text not null,nYear text not null,nNumValdm text not null,recordid text not null);");

			mydb.execSQL("create table if not exists "
					+ FDDBNOTE_TABLE
					+ "(_id integer primary key autoincrement,RefNo text not null,TxnDate text not null,BAmt text not null,DebCode text not null,RecordId text not null,ManuRef text not null,Curcode text not null,Amt text not null,Totbal text not null,Remarks text not null,RepCode text not null);");

			mydb.execSQL("create table if not exists "
					+ FAREA_TABLE
					+ "(fArea_id integer primary key autoincrement,AddDate text not null,AddMach text not null,AddUser text not null,AreaCode text not null,AreaName text not null,RecordId text not null);");

			// *****Sales Order Transaction Table *****
			mydb.execSQL("create table if not exists "
					+ FORDHED_TABLE
					+ "(ordhed_id integer primary key autoincrement,RefNo text not null,RefNo1 text not null,TxnDate text not null,ManuRef text not null,CurCode text not null,CostCode text not null,CurRate text not null,DebCode text not null,Remarks text not null,TxnType text not null,QuoTerm text not null,LocCode text not null,RepCode text not null,BTotalDis text not null,TotalDis text not null,PTotalDis text not null,BPTotalDis text not null,BTotalTax text not null,TotalTax text not null,BTotalAmt text not null,TotalAmt text not null,TaxReg text not null,Contact text not null,CusAdd1 text not null,CusAdd2 text not null,CusAdd3 text not null,CusTele text not null,PrtCopy text not null,AddUser text not null,AddDate text not null,AddMach text not null,disper text not null,Appsts text not null,SellSts text not null,AppUser text not null,Appdate text not null,RemarkPri text not null,Customer_name text not null,Active_State text not null,SalesOrd_Type text not null,Longitude text not null,Latitude text not null,InvStartTime text not null,InvEndTime text not null,InvHedNetTotal text not null,syncHed text not null,salesOrdStartTime text not null,salesOrdEndTime text not null);");

			mydb.execSQL("create table if not exists "
					+ FORDDET_TABLE
					+ "(orddet_id integer primary key autoincrement,RefNo  text not null,TxnDate  text not null,TxnType  text not null,SeqNo text not null,Itemcode text not null,Qty text not null,BalQty text not null,TaxComCode text not null,BSellPrice text not null,SellPrice text not null,BTSellPrice text not null,TSellPrice text not null,PrilCode text not null,DisPer text not null,BDisAmt text not null,DisAmt text not null,PDisAmt text not null,BPDisAmt text not null,BTaxAmt text not null,TaxAmt text not null,BAmt text not null,Amt text not null,Linkrec text not null,OrSellPrice text not null,OrBSellPrice text not null,OrTSellPrice text not null,OrBTSellPrice text not null,Packing text not null,Quopacking text not null,cases text not null,freeqty text not null,balfreeqty text not null,ItemName text not null,RetailPrice text not null,BrandCode text not null,BrandName text not null,value text not null,totordval text not null,finalQty text not null,syncDet text not null,itemoriSalePrice text not null,itemTaxRate text not null);");

			mydb.execSQL("create table if not exists "
					+ FORDTAXDT_TABLE
					+ "(taxdet_id integer primary key autoincrement,RefNo text not null,ItemCode text not null,TaxComCode text not null,TaxCode text not null,TaxPer text not null,TaxRate text not null,Taxseq text not null,BTaxDetAmt text not null,TaxDetAmt text not null);");

			mydb.execSQL("create table if not exists "
					+ FORDTAXRG_TABLE
					+ "(taxdetrg_id integer primary key autoincrement,RefNo text not null,TaxCode text not null,TaxRegNo text not null);");

			mydb.execSQL("create table if not exists "
					+ FCONTROL_TABLE
					+ "(fcontrol_id integer primary key autoincrement,AdCntAcc text not null,AutoAst text not null,BalgCrLm text not null,ComAdd1 text not null,ComAdd2 text not null,ComName text not null,Comgainlos text not null,ContTxn text not null,CrosAccFlg text not null,DealCode text not null,IntegPrefx text not null,IntegSeqNo text not null,NbtCmTaxNo text not null,RecordId text not null,SysId text not null,Tranlock text not null,VatCmTaxNo text not null,aRetEarn text not null,basecur text not null,comRegNo text not null,comemail text not null,comfax1 text not null,comgstac text not null,comtel1 text not null,comtel2 text not null,comweb text not null);");
			mydb.execSQL("create table if not exists "
					+ ITEMMASTER_SUM
					+ "(itmsum_id integer primary key autoincrement,ItemCode text not null,ItmSumaddDate text not null);");

			mydb.execSQL("create table if not exists "
					+ FGLFACC_TABLE
					+ "(fglfacc_id integer primary key autoincrement,AccCat text not null,AccCode text not null,AccDesc text not null,AccTyp text not null,AddDate text not null,AddMach text not null,AddUser text not null,BankChg text not null,ConAcc text not null,CostCode text not null,CurCode text not null,NoteCode text not null,NumGen text not null,RecordId text not null,SortCode text not null,SubnNote text not null);");

			mydb.execSQL("create table if not exists "
					+ FBANK_TABLE
					+ "(fbank_id integer primary key autoincrement,AddDate text not null,AddMach text not null,	AddUser text not null,Branch  text not null,RecordId text not null,bankaccno text not null,bankadd1 text not null,bankadd2 text not null,bankcode text not null,bankname text not null);");

			mydb.execSQL("create table if not exists "
					+ FPRECHED_TABLE
					+ "(fpreched_id integer primary key autoincrement,RefNo text not null,ManuRef text not null,SaleRefNo text not null,RepCode text not null,TxnType text not null,Chqno text not null,ChqDate text not null,TxnDate text not null,CurCode text not null,CurRate1 text not null,DebCode text not null,TotalAmt text not null,BTotalAmt text not null,BankCur text not null,BankRate text not null,BankAmt text not null,BankCharge text not null,BBankChg text not null,GnLsCal text not null,DbAcc text not null,PayType text not null,PrtCopy text not null,GlPost text not null,Remarks text not null,CardSlipNo text not null,ExpDate text not null,CrdTypCode text not null,CCBankAmt text not null,GlBatch text not null,AddUser text not null,AddMach text not null,AddDate text not null,CurRate text not null,customer_name text not null,payTypeDes text not null,CACHAcNo text not null,CACHAcBank text not null,CusBankCode text not null,CusBankName text not null,ActiveState text not null,SyncState text not null,AllocatedOriAmount text not null,AllocatedAmount text not null);");

			mydb.execSQL("create table if not exists "
					+ FPRECDET_TABLE
					+ "(fprecdet_id	integer primary key autoincrement,RefNo text not null,RefNo1 text not null,SaleRefNo text not null,ManuRef text not null,TxnType text not null,TxnDate text not null,DtxnDate text not null,DtxnType text not null,DCurCode text not null,DCurRate text not null,OCurRate text not null,RepCode text not null,Amt text not null,BAmt text not null,GainLosAmt text not null,BnkChgs text not null,BBnkChgs text not null,AloAmt text not null,OvPayAmt text not null,OvPayBal text not null,SyncState text not null);");

		} catch (Exception e) {
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase mydb, int oldVersion, int newVersion) {
		// on upgrade drop older tables
	/*	String column_name_sa = "itemoriSalePrice";
		mydb.execSQL("ALTER TABLE " + FORDDET_TABLE + " ADD COLUMN "
				+ column_name_sa);*/
		//String column_name_sa = "ItemStatus"; - VERSION - 02
		String column_name_sa = "FreeItems";
		mydb.execSQL("ALTER TABLE " + FITEMS_TABLE + " ADD COLUMN "
				+ column_name_sa);
		Log.v(TAG, "onUpgrade Call");
	}
}
